package com.itheima.zhaohang.mymoreitemsclicke;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima.zhaohang.adapter.BasicRecycleViewAdapter;
import com.itheima.zhaohang.adapter.MainAdapter;
import com.ufo.dwrefresh.view.DWRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private DWRefreshLayout mDwRefreshLayout;
    private RecyclerView mRecyclerView;
    private Toolbar mToolbar;
    private TextView mTextView;
    private MyToobar mMyToobar;
    private String mane = "对不起，我们只卖真皮";

    private List<String> mStrings = new ArrayList<>();
    private Handler mHandler = new Handler(Looper.getMainLooper()){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mDwRefreshLayout.setRefresh(false);
        }
    };
    private MainAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //预先设置主题属性为NoActionBar
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mTextView = (TextView) findViewById(R.id.titleme);
        //对系统原生的actionBar进行操作
        initActionBar();


        mDwRefreshLayout = (DWRefreshLayout) findViewById(R.id.dwRefreshLayout);
        mMyToobar = (MyToobar) findViewById(R.id.mytoobar);

        mMyToobar.setOnIconClickeListener(new MyToobar.OnClickeListener() {
            @Override
            public void onImageViewClickeListener() {
             ToastUtils.showToast(MainActivity.this, "点击图标了");
            }

            @Override
            public void onTextViewClickeListener() {
                ToastUtils.showToast(MainActivity.this, "点击文字了");
            }
        });
        mMyToobar.getText(mane);
        //也可以直接拿过来来设置属性，上面的方法跟下面的差不多都一样的
        //  TextView viewById = mMyToobar.findViewById(R.id.toobartext_use);
        TextView viewById = mMyToobar.getChildTextView();
        viewById.setTextSize(50);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);

        mDwRefreshLayout.setOnRefreshListener(new DWRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //刷新回调
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mHandler.sendEmptyMessage(0);
                            }
                        }, 2000);
                    }
                });
            }

            @Override
            public void onLoadMore() {
                //加载更多回调
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mHandler.sendEmptyMessage(0);
                            }
                        }, 2000);

                    }
                });
            }
        });
        for (int i = 0; i < 50; i++) {
            if (i % 2 == 0) {
                mStrings.add("我在左边");
            } else {
                mStrings.add("我在右边");
            }

        }


        //对RecycleView进行数据添加
        inData();
    }


    /**
     * 对原生的actionBar进行操作
     */
    private void initActionBar() {

        //预先设置主题文字为没有文字状态
        mToolbar.setTitle("哈哈");
        //设置新的actionBar替代老的actionbar
        setSupportActionBar(mToolbar);
    }
    //设置系统的actionbar,添加主题布局
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main_menu,menu);
        return true;
    }
    //设置添加主题图标
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuBuilder builder = (MenuBuilder) menu;
        builder.setOptionalIconsVisible(true);
        return true;
    }
    //条目的点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_and_frient:
                Toast.makeText(this,"添加朋友",Toast.LENGTH_SHORT).show();
            case R.id.menu_share_frient:
                Toast.makeText(this,"分享朋友",Toast.LENGTH_SHORT).show();
            case R.id.menu_about_us:
                Toast.makeText(this,"关于我们",Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    /**
     * 对recycleview进行数据添加
     */
    private void inData() {
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new MainAdapter(mStrings);
        //添加布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
        //添加布局监听器
        mAdapter.setOnItemClickeListener(R.id.fisrt, new BasicRecycleViewAdapter.ItemClickeListener() {
            @Override
            public void onItemClicked(View view, int position) {
            //Toast.makeText()
            ToastUtils.showToast(MainActivity.this,"这是右边的第"+position+"条文字");
            }
        });
        mAdapter.setOnItemClickeListener(R.id.right_image,new BasicRecycleViewAdapter.ItemClickeListener(){

            @Override
            public void onItemClicked(View view, int position) {
                ToastUtils.showToast(MainActivity.this,"这是右边的第"+position+"图片");
            }
        });
        mAdapter.setOnItemClickeListener(R.id.lift_text, new BasicRecycleViewAdapter.ItemClickeListener() {
            @Override
            public void onItemClicked(View view, int position) {
                ToastUtils.showToast(MainActivity.this,"这是左边的第"+position+"条文字");
            }
        });
        mAdapter.setOnItemClickeListener(R.id.lift_imageview,new BasicRecycleViewAdapter.ItemClickeListener(){

            @Override
            public void onItemClicked(View view, int position) {
                ToastUtils.showToast(MainActivity.this,"这是左边的第"+position+"图片");
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAdapter.destroyAdapter();
    }


}
//  System.out.println("我已经请求数据了");
//          //我们需要使用缓存框架拿到数据
//          //http://127.0.0.1:8090/home?index=0
//          //数据格式
//          //请求的服务在手机端不需要权限,一般不可能 出现这种情况.
//
//          //得到当前的模式
//          //如果当前的模式是下拉,那么集合清空
//          //下拉模式
//          if (mPullRefreshList.getCurrentMode() == PullToRefreshBase.Mode.PULL_FROM_START)
//          {
//          //清空数据
//          mShowItems.clear();
//          }

            //根据与后台的协定，上啦uir是0，向下刷新uir是+20或者10，也就是集合的长度
//          获取数据的方法
//          HomeItem homeItem = CacheManager.getInstance().getBeanData(Uris.HOMEURL+mShowItems.size(), HomeItem.class);
//
//        if (homeItem == null) {
//        return homeItem;
//        }
//
//        //更新集合
//        mShowItems.addAll(homeItem.getList());
//
//        //更新adapter
//        Utils.runOnUIThread(new Runnable() {
//@Override
//public void run() {
//        //更新列表
//        mHomeAdapter.notifyDataSetChanged();
//
//        //停止下拉刷新
//        mPullRefreshList.onRefreshComplete();
//        }
//        });
//
//
//        return mShowItems;
//        }